﻿namespace PlaylistCopy.Services.Impl
{
    using CuttingEdge.Conditions;
    using Microsoft.Win32;
    using Ookii.Dialogs.Wpf;
    using System;
    using System.Windows;
    using Condition = CuttingEdge.Conditions.Condition;

    internal class DialogService : IDialogService
    {
        public string OpenSingleFile(string initDirectory, string filter)
        {
            var openFileDialog = new OpenFileDialog
                                 {
                                     Filter = filter,
                                     InitialDirectory = initDirectory,
                                     Multiselect = false
                                 };

            var confirmed = openFileDialog.ShowDialog() ?? false;

            return confirmed ? openFileDialog.FileName : null;
        }

        public string OpenFolder(string description = null)
        {
            var openFolderDialog = new VistaFolderBrowserDialog
                                   {
                                       UseDescriptionForTitle = true,
                                       ShowNewFolderButton = true,
                                       Description =
                                           description
                                   };

            var result = openFolderDialog.ShowDialog();

            return result != null && result.Value ? openFolderDialog.SelectedPath : null;
        }

        public string SaveSingleFile(string initDirectory, string filter)
        {
            var saveFolderDialog = new VistaSaveFileDialog
                                   {
                                       Filter = filter,
                                       InitialDirectory = initDirectory,
                                   };

            var confirmed = saveFolderDialog.ShowDialog() ?? false;

            return confirmed ? saveFolderDialog.FileName : null;
        }

        public void ProgressDialog(Action<IWorkArguments> work, string title = null, string description = null)
        {
            Condition.Requires(work, "work").IsNotNull();

            var progDialog = new ProgressDialog
                             {
                                 ShowTimeRemaining = true,
                                 WindowTitle = title,
                                 Description = description
                             };

            var workArgs = new WorkArguments(progDialog);
            progDialog.DoWork += (sender, args) => work(workArgs);

            progDialog.ShowDialog();
        }

        public void ShowDialog(string title, string message, MessageBoxButton buttons, MessageBoxImage image)
        {
            MessageBox.Show(message, title, buttons, image);
        }

        public class WorkArguments : IWorkArguments
        {
            private readonly ProgressDialog progressDialog;

            public WorkArguments(ProgressDialog progressDialog)
            {
                Condition.Requires(progressDialog, "progressDialog").IsNotNull();

                this.progressDialog = progressDialog;
            }

            public bool IsCancelling
            {
                get { return this.progressDialog.CancellationPending; }
            }

            public void ReportProgress(int percent, string text, string description)
            {
                this.progressDialog.ReportProgress(percent, text, description);
            }
        }
    }
}
